# Makefile

MAKEFLAGS += --no-print-directory

# Clear the default suffixes
.SUFFIXES:
# Don't delete intermediate files
.SECONDARY:
# Delete files that weren't built properly
.DELETE_ON_ERROR:

MAC = $(shell uname -s | grep -i -q 'darwin'; echo $$?)

ifneq ($(MAC), 0)
# see if on msys2, but only if not on mac because /proc/version doesn't exist there
MSYS2 = $(shell grep -i -q 'msys' /proc/version; echo $$?)
else
MSYS2 = 1
endif

BUILD := build
BUILD_NARC := output/src/a

# environment setting
# get rid of devkitpro:  if devkitpro is installed, can still use it.  otherwise, default to arm-none-eabi tools
ifeq ($(shell echo $$DEVKITARM),)
ifeq ($(MSYS2), 0)
PREFIX = /mingw64/bin/arm-none-eabi-
AS = $(PREFIX)as
CC = $(PREFIX)gcc
LD = $(PREFIX)ld
OBJCOPY = $(PREFIX)objcopy
else
PREFIX = arm-none-eabi-
AS = $(PREFIX)as
CC = $(PREFIX)gcc
LD = $(PREFIX)ld
OBJCOPY = $(PREFIX)objcopy
endif
else
# support legacy devkitpro instructions
PREFIX = bin/arm-none-eabi-
AS = $(DEVKITARM)/$(PREFIX)as
CC = $(DEVKITARM)/$(PREFIX)gcc
LD = $(DEVKITARM)/$(PREFIX)ld
OBJCOPY = $(DEVKITARM)/$(PREFIX)objcopy
endif
PYTHON = python3

GFX := tools/nitrogfx/nitrogfx
NARCHIVE := tools/knarc/knarc
BTX := $(PYTHON) tools/overworld-btx.py

.PHONY: clean all cpfile itemicon


default: all

include narc.mk
include data/graphics/pokegra.mk

SUBDIRS  := $(sort $(dir $(NARC_FILES) $(dir $(OBJS)) $(dir $(ALL_OVERWORLDS_OBJS))))
$(shell mkdir -p $(SUBDIRS))

nitrogfx:
	mkdir -p build/pokemonpic
	mkdir -p build/pokemonicon
	make -C tools/nitrogfx/
	make -C tools/knarc/

all: nitrogfx $(NARC_FILES)

clean:
	rm -rf $(BUILD)
	rm -rf output

cpfile: all
	cp -r output/. ../new-ahg/